package com.shicijiaoliu.controller;

import com.shicijiaoliu.pojo.Mainpost;
import com.shicijiaoliu.pojo.Post;
import com.shicijiaoliu.pojo.Replypost;
import com.shicijiaoliu.pojo.result;
import com.shicijiaoliu.service.Mainpostservice;
import com.shicijiaoliu.service.Replyservice;
import com.shicijiaoliu.service.Sectionservice;
import com.shicijiaoliu.service.Userservice;
import com.shicijiaoliu.utils.ThreadLocalUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/mpost")
@Validated
public class mainpostcontroller {
    @Autowired
    private Mainpostservice mainpostservice;

    @Autowired
    private Userservice userservice;

    @Autowired
    private Sectionservice sectionservice;

    @Autowired
    private Replyservice replyservice;

    @GetMapping("/list")
    public result<Object> list(int countpage, int pagenumber,
                               @RequestParam(required = false) String title,
                               @RequestParam(required = false) String username,
                               @RequestParam(required = false) String sectionname){
        long aId = 0; long sId = 0;
        if(username!=null){
            if(userservice.findByName(username) == null){
                return result.error("用户不存在，请重新搜索");
            }else{
                aId = userservice.findByName(username).getId();
            }
        }
        if(sectionname!=null){
            if(sectionservice.findByname(sectionname) == null){
                return result.error("版块不存在，请重新搜索");
            }else{
                sId = sectionservice.findByname(sectionname).getSId();
            }
        }
        List<Mainpost> mainpostlist = mainpostservice.findBypage(countpage,pagenumber,title,aId,sId);
        for (Mainpost mainpost : mainpostlist) {
            String uname = userservice.findByid(mainpost.getAId()).getUsername();
            String sname = sectionservice.findByid(mainpost.getSId()).getName();
            mainpost.setUsername(uname);
            mainpost.setSectionname(sname);
        }
        if(mainpostlist == null){
            return result.error("获取页大于总页数");
        }else{
            int count = mainpostservice.getcount(title,aId,sId);
            Map<String,Object> data = new HashMap<>();
            data.put("total",count);
            data.put("items",mainpostlist);
            return result.success(data);}
    }



    @DeleteMapping("/del/{id}")
    public result del(@PathVariable("id")long mId){
        if (mainpostservice.findByid(mId) == null) {
            return result.error("没有找到该主贴");
        } else {
            mainpostservice.delByid(mId);
            return result.success();
        }
    }

    @PatchMapping("/jia/{id}")
    public result jia(@PathVariable("id")long mId){
        if (mainpostservice.findByid(mId) == null) {
            return result.error("没有找到该主贴");
        } else {
            mainpostservice.jiajing(mId);
            return result.success();
        }
    }

    @PatchMapping("/jubao/{id}")
    public result jubao(@PathVariable("id")long mId){
        if (mainpostservice.findByid(mId) == null) {
            return result.error("没有找到该主贴");
        } else {
            System.out.println(mId);
            mainpostservice.jubao(mId);
            return result.success();
        }
    }

    @GetMapping("/post")
    public result<Object> post(){
        long id = 0;
        List<Mainpost> mpost = mainpostservice.getlist();
        List<Replypost> rpost = replyservice.getlist();
        List<Post> post = new ArrayList<>();
        for (Mainpost mainpost : mpost) {
            id++;
            String name = userservice.findByid(mainpost.getAId()).getUsername();
            String content = mainpost.getContent();
            LocalDateTime time = mainpost.getPostingtime();
            Long click = mainpost.getClicks();
            String title = mainpost.getTitle();
            Post p = new Post(name, content, time, click,title,id);
            post.add(p);
        }
        for (Replypost replypost : rpost) {
            id++;
            String name = userservice.findByid(replypost.getAId()).getUsername();
            String content = replypost.getContent();
            LocalDateTime time = replypost.getTime();
            Long click = replypost.getClicks();
            String title = content.substring(0,2) + "...";
            Post p = new Post(name, content, time, click,title,id);
            post.add(p);
        }

        return result.success(post);


    }

    @GetMapping("/mposts/{sectionname}")
    public result<Object> mpost(@PathVariable("sectionname")String sectionname){

        long sid = sectionservice.findByname(sectionname).getSId();
        List<Mainpost> mposts = mainpostservice.findBysid(sid);
        for (Mainpost mainpost : mposts) {
            String uname = userservice.findByid(mainpost.getAId()).getUsername();
            String sname = sectionservice.findByid(mainpost.getSId()).getName();
            mainpost.setUsername(uname);
            mainpost.setSectionname(sname);
        }
        long clicks = 0;
        for (Mainpost x : mposts){
            clicks += x.getClicks();
        }
        Map<String,Object> data = new HashMap<>();
        data.put("data",mposts);
        data.put("num",mposts.size());
        data.put("clicks",clicks);
        return result.success(data);

    }

    

    @PutMapping("/update/{mid}")
    public result update(long sid,long aid,LocalDateTime finaltime,@PathVariable("mid")long mid,Long clicks,LocalDateTime postingtime,String content,String title,Long replies,String isjing){
        Mainpost mpost = new Mainpost(aid,sid,mid,finaltime,clicks,postingtime,content,title,replies,isjing);
        if (mainpostservice.findByid(mid) == null) {
            return result.error("没有找到该主贴");
        } else {
            mainpostservice.update(mpost);
            return result.success();
        }
    }

    @PostMapping("/add")
    public result add(String content,String sectionname,String title){
        long sid = sectionservice.findByname(sectionname).getSId();
        System.out.println(sid);
        Map<String,Object> claims = ThreadLocalUtils.getThreadLocal();
        long aid = Long.parseLong((String) claims.get("id"));
        Mainpost mpost = new Mainpost(content,sid,aid,title);
        mainpostservice.add(mpost);
        return result.success();
    }


}
